package com.neusoft.yuejian12.yuejian_12.fragment;



import android.content.Intent;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.neusoft.yuejian12.yuejian_12.R;
import com.neusoft.yuejian12.yuejian_12.activity.NewPostActivity;
import com.neusoft.yuejian12.yuejian_12.adapter.PostListAdapter;
import com.neusoft.yuejian12.yuejian_12.javabean.PostBean;
import com.neusoft.yuejian12.yuejian_12.listener.RetrofitListener;
import com.neusoft.yuejian12.yuejian_12.model.PostModel;

import java.util.List;


public class PostFragment extends Fragment implements RetrofitListener<List<PostBean>> {

    private RecyclerView recyclerView;
    private View view = null;
    private TextView AddPost;
    private PostListAdapter postListAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        view = inflater.inflate(R.layout.tab04, container,false);
        recyclerView = (RecyclerView) view.findViewById(R.id.rV_post);
        recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        SpacesItemDecoration decoration=new SpacesItemDecoration(6);
        recyclerView.addItemDecoration(decoration);
        getData();
        initViews();
        initEvevnts();
        return view;
    }
    private void initViews(){
        AddPost = (TextView) view.findViewById(R.id.addpost);
    }
    private void initEvevnts(){
        AddPost.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
               Intent intent = new Intent(getActivity(),NewPostActivity.class);
                startActivity(intent);
            }
        });
    }

    @Override
    public void onSuccess(List<PostBean> postBean) {
        postListAdapter = new PostListAdapter(PostFragment.this.getActivity(),postBean);
        recyclerView.setAdapter(postListAdapter);

    }

    @Override
    public void onFail() {

    }

    public class SpacesItemDecoration extends RecyclerView.ItemDecoration{
         private int space;

         public SpacesItemDecoration(int space){
             this.space=space;
         }

         @Override
         public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
            outRect.left=space;
             outRect.right=space;
             outRect.bottom=space;
             if (parent.getChildAdapterPosition(view)==0){
                 outRect.top=space;
             }
         }
     }

    private void getData(){
        PostModel postModel=new PostModel();
        postModel.getPostlist(this);
    }

    @Override
    public void registerForContextMenu(View view) {
        super.registerForContextMenu(view);
    }

}
